package cn.lili.modules.goods.serviceimpl;

import cn.hutool.core.collection.CollUtil;
import cn.lili.cache.Cache;
import cn.lili.cache.CachePrefix;
import cn.lili.modules.goods.entity.dos.Wholesale;
import cn.lili.modules.goods.entity.dto.WholesaleDTO;
import cn.lili.modules.goods.mapper.WholesaleMapper;
import cn.lili.modules.goods.service.WholesaleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 批发规则业务层实现
 *
 * @author paulG
 * @since 2022/5/24
 **/
@Service
@RequiredArgsConstructor
public class WholesaleServiceImpl extends ServiceImpl<WholesaleMapper, Wholesale> implements WholesaleService {

    private final Cache<List<Wholesale>> cache;


    @Override
    @Transactional
    public void saveWholesale(String goodsId, List<WholesaleDTO> wholesaleDTOS) {
        //先删除,再存储。这里由于方法内调用，
        this.removeByGoodsId(goodsId);
        if (CollUtil.isNotEmpty(wholesaleDTOS)) {
            List<Wholesale> wholesaleList = wholesaleDTOS.stream().map(wholesaleDTO -> {
                Wholesale wholesale = new Wholesale();
                wholesale.setGoodsId(goodsId);
                wholesale.setNum(wholesaleDTO.getNum());
                wholesale.setPrice(wholesaleDTO.getPrice());
                return wholesale;
            }).toList();
            this.saveBatch(wholesaleList);
        }
    }

    @Override
    @Transactional
    public void saveWholesaleOrg(String goodsId, List<Wholesale> wholesales) {
        //先删除,再存储。这里由于方法内调用，
        this.removeByGoodsId(goodsId);
        if (CollUtil.isNotEmpty(wholesales)) {
            List<Wholesale> wholesaleList = wholesales.stream().map(wholesaleDTO -> {
                Wholesale wholesale = new Wholesale();
                wholesale.setGoodsId(goodsId);
                wholesale.setNum(wholesaleDTO.getNum());
                wholesale.setPrice(wholesaleDTO.getPrice());
                return wholesale;
            }).toList();
            this.saveBatch(wholesaleList);
        }
    }

    @Override
    public List<Wholesale> findByGoodsId(String goodsId) {
        LambdaQueryWrapper<Wholesale> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Wholesale::getGoodsId, goodsId);
        return this.list(queryWrapper).stream().sorted(Comparator.comparing(Wholesale::getNum)).collect(Collectors.toList());
    }

    @Override
    public Boolean removeByGoodsId(String goodsId) {
        LambdaQueryWrapper<Wholesale> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Wholesale::getGoodsId, goodsId);
        cache.remove(key(goodsId));
        return this.remove(queryWrapper);
    }

    @Override
    public Wholesale match(String goodsId, Integer num) {
        List<Wholesale> wholesaleList = cache.get(key(goodsId));
        if (wholesaleList == null) {
            wholesaleList = this.findByGoodsId(goodsId);
            if (CollUtil.isEmpty(wholesaleList)) {
                return null;
            }
            cache.put(key(goodsId), wholesaleList);
        }
        List<Wholesale> matchList = wholesaleList.stream()
                .filter(wholesale -> wholesale.getNum() <= num)
                .collect(Collectors.toList());
        if (CollUtil.isNotEmpty(matchList)) {
            return matchList.get(matchList.size() - 1);
        } else if (CollUtil.isNotEmpty(wholesaleList) && CollUtil.isEmpty(matchList)) {
            return wholesaleList.get(0);
        }
        return null;
    }

    /**
     * 获取缓存key
     *
     * @param goodsId 商品id
     * @return 缓存key
     */
    static String key(String goodsId) {
        return CachePrefix.WHOLESALE.getPrefix() + "_" + goodsId;
    }
}
